using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour
{
    public Weapon[] primaryWeapons;
    public Weapon[] secondaryWeapons;
    private int primaryWeaponIndex = 0;
    private int secondaryWeaponIndex = 0;
    public Transform[] primaryTubes;
    public Transform[] secondaryTubes;
    public Transform pointer;
    public int currentprimaryTubeIndex = 0;
    public int currentsecondaryTubeIndex = 0;
    public float primaryWeaponWait = 0;
    public float secondaryWeaponWait = 0;

    string player;
    // Use this for initialization
    void Start()
    {
        foreach (Weapon p in primaryWeapons)
        {
            p.Init();
        }
        foreach (Weapon s in secondaryWeapons)
        {
            s.Init();
        }
    } 
    public void disable(float timespan)
    {
        primaryWeaponWait = timespan;
        secondaryWeaponWait = timespan;
    }
    private Transform getNextprimaryTube()
    {
        Transform t = primaryTubes[currentprimaryTubeIndex];
        currentprimaryTubeIndex = ++currentprimaryTubeIndex % primaryTubes.Length;
        return t;
    }
    private Transform getNextsecondaryTube()
    {
        Transform t = secondaryTubes[currentsecondaryTubeIndex];
        currentsecondaryTubeIndex = ++currentsecondaryTubeIndex % secondaryTubes.Length;
        return t;
    }
    public bool shooting = false;
    // Update is called once per frame
    private void decreaseWeaponWait()
    {
        primaryWeaponWait -= Time.deltaTime;
        if (primaryWeaponWait < 0)
        {
            primaryWeaponWait = 0;
        }
        secondaryWeaponWait -= Time.deltaTime;
        if (secondaryWeaponWait < 0)
        {
            secondaryWeaponWait = 0;
        }
    }
    private Vector3 aim()
    {
        RaycastHit hit;
        
        Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/ 2, Screen.height / 2, 0));
        Debug.DrawRay(transform.position, transform.forward*10,Color.blue);
        if (Physics.Raycast(ray, out hit))
        {
            return hit.point;
        }
        return pointer.position;
    }
    private void Fire(bool primary)
    {
        Transform nextTube;
        Weapon prefab;
        string key = primary ? "Fire1" : "Fire2";
        float wait = primary ? primaryWeaponWait : secondaryWeaponWait;
        float count = primary ? primaryWeapons[primaryWeaponIndex].count : secondaryWeapons[secondaryWeaponIndex].count;
        if (Input.GetAxis(key) == 1)
        {
            if (wait == 0 && count > 0)
            {
                nextTube = primary ? getNextprimaryTube() : getNextsecondaryTube();
                prefab = primary ? primaryWeapons[primaryWeaponIndex] : secondaryWeapons[secondaryWeaponIndex];
                Weapon bullet = (Weapon)Network.Instantiate(prefab, nextTube.position + nextTube.forward, transform.rotation, 0);
                bullet.sender = gameObject;
                bullet.transform.LookAt(aim());
                bullet.rigidbody.AddForce(bullet.transform.forward * bullet.force, ForceMode.Force);
                if (primary)
                {
                    primaryWeapons[primaryWeaponIndex].count--;
                    primaryWeaponWait = bullet.getCurrentAttackInterval();
                } 
                else
                {
                    secondaryWeapons[secondaryWeaponIndex].count--;
                    secondaryWeaponWait = bullet.getCurrentAttackInterval();
                }

            }
        }


    }
    void FixedUpdate()
    {
        if (!networkView.isMine)
        {
            return;
        }
        decreaseWeaponWait();
        if (Input.GetAxis("Mouse ScrollWheel") == 1)
        {
            primaryWeaponIndex = changeIndex(true, primaryWeaponIndex, primaryWeapons.Length);

        }
        if (Input.GetAxis("Mouse ScrollWheel") == -1)
        {
            primaryWeaponIndex = changeIndex(false, primaryWeaponIndex, primaryWeapons.Length);
        }
        Fire(true);
        Fire(false); 
    }
    void OnGUI()
    {
        Weapon p = primaryWeapons[primaryWeaponIndex];
        Weapon s = secondaryWeapons[secondaryWeaponIndex];
        GUITexture pimage = GameObject.Find("PrimaryWeaponImage").guiTexture;

        GUIText pimagecount = GameObject.Find("PrimaryWeaponCount").guiText;
        GUIText pimagespezial = GameObject.Find("PrimaryWeaponSpezial").guiText;

        GUIText pimagedamage = GameObject.Find("PrimaryWeaponDamage").guiText;

        GUIText pimageas = GameObject.Find("PrimaryWeaponAttackSpeed").guiText;


        pimage.texture = p.image;
        p.image.Play();
        pimagecount.text = p.count + " Ammo";
         if (p.slowChance > 0)
        {
            pimagespezial.text = p.slowChance + " % chance to slow " + p.slowPercentage + "% for " + p.slowTimespan + " sec";
        }
        if (p.stunChance > 0)
        {
            pimagespezial.text = p.stunChance + " % chance to stun for " + p.stunTimespan + " sec";
        }
        if (p.poisonChance > 0)
        {
            pimagespezial.text = p.poisonChance + " % chance to poisen for " + p.poisonTimespan + " sec. deals " + p.poisonAmount + " dmg every " + p.poisonDelay + " sec";
        }

        if (p.decreaseAttackSpeedChance > 0)
        {
            pimagespezial.text = p.decreaseAttackSpeedChance + " % chance to decrease attackspeed by " + p.decreaseAttackSpeedPercentage + "% for " + p.decreaseAttackSpeedTimespan + " sec";
        }

        pimagedamage.text = p.damage + " Damage";
        pimageas.text = p.attackSpeed + " AttackSpeed";

        GUITexture simage = GameObject.Find("SecondaryWeaponImage").guiTexture;
        GUIText simagecount = GameObject.Find("SecondaryWeaponCount").guiText;
        GUIText simagespezial = GameObject.Find("SecondaryWeaponSpezial").guiText;

        GUIText simagedamage = GameObject.Find("SecondaryWeaponDamage").guiText;

        GUIText simageas = GameObject.Find("SecondaryWeaponAttackSpeed").guiText;
        simage.texture = s.image;
        simagecount.text = s.count + " Ammo";
        if (s.stunChance > 0)
        {
            simagespezial.text = s.stunChance + " % chance to stun for " + s.stunTimespan + " sec";
        }
        if (s.poisonChance > 0)
        {
            simagespezial.text = s.poisonChance + " % chance to poisen for " + s.poisonTimespan + " sec. deals " + s.poisonAmount + " dmg every " + s.poisonDelay + " sec";
        }
        if (s.slowChance > 0)
        {
            simagespezial.text = s.slowChance + " % chance to slow " + s.slowPercentage + "% for " + s.slowTimespan + " sec";
        }
        simagedamage.text = s.damage + " Damage";
        if (s.decreaseAttackSpeedChance > 0)
        {
            simagespezial.text = s.decreaseAttackSpeedChance + " % chance to decrease attackspeed by " + s.decreaseAttackSpeedPercentage + "% for " + s.decreaseAttackSpeedTimespan + " sec";
        }
        simageas.text = s.attackSpeed + " AttackSpeed";
    }
    int changeIndex(bool increase, int index, int size)
    {
        if (increase)
        {
            return ++index % size;
        }
        else
        {
            if (--index < 0)
            {
                return size - 1;
            }
            else
            {
                return index;
            }
        }
    }

}
